package com.yuanfud.zuulServer.config;

import com.yuanfud.common.exception.ErrorInfo;
import com.yuanfud.common.exception.RestException;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Configuration
@ControllerAdvice
public class GlobalException {
    @ExceptionHandler(value = RestException.class)
    @ResponseBody
    public ErrorInfo jsonErrorHandler(HttpServletRequest req, RestException e) throws Exception {
        ErrorInfo errorInfo = new ErrorInfo();

        errorInfo.setCode(e.getCode());
        errorInfo.setMessage(e.getMessage());
        return errorInfo;
    }
}
